﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace AVCUMPlatform.WXService.Model
{
    /// <summary>
    /// 微信配置信息
    /// </summary>
    public class WXConfig
    {
        /// <summary>
        /// 微信配置单例
        /// </summary>
        private static WXConfig instance = new WXConfig();
        /// <summary>
        /// 随机对象
        /// </summary>
        private Random random = new Random();
        /// <summary>
        /// 微信应用id
        /// </summary>
        private string _appId { get; set; }
        /// <summary>
        /// 微信密钥
        /// </summary>
        private string _appSecret { get; set; }

        /// <summary>
        /// 私有构造
        /// </summary>
        private WXConfig() { }

        /// <summary>
        /// 获取单例
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="appsecret"></param>
        /// <returns></returns>
        public static WXConfig GetInstance(string appid = null, string appsecret = null)
        {
            instance._appId = appid;
            instance._appSecret = appsecret;
            return instance;
        }

        /// <summary>
        /// 获取配置
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public WxConfigEntity getWxConfig(string url)
        {
            WxConfigEntity config = new WxConfigEntity();
            config.NonceStr = NextString(15, true);
            config.Timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            config.AppId = _appId;
            config.Token = WXToken.GetInstance(_appId, _appSecret).AccessToken;
            config.Ticket = WXTicket.GetInstance(_appId, _appSecret).JSApiTicket;
            var content = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}",
                config.Ticket,
                config.NonceStr,
                config.Timestamp,
                url);

            config.Signature = SHA1_Hash(content);
            return config;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str_sha1_in"></param>
        /// <returns></returns>
        private string SHA1_Hash(string str_sha1_in)
        {
            SHA1 sha1 = SHA1.Create();
            byte[] bytes_sha1_in = UTF8Encoding.Default.GetBytes(str_sha1_in);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            str_sha1_out = str_sha1_out.Replace("-", "").ToLower();
            return str_sha1_out;
        }

        /// <summary>
        /// 获取随机串
        /// </summary>
        /// <param name="size"></param>
        /// <param name="lowerCase"></param>
        /// <returns></returns>
        private string NextString(int size, bool lowerCase)
        {
            StringBuilder _builder = new StringBuilder(size);
            int _startChar = lowerCase ? 97 : 65;//65 = A / 97 = a
            for (int i = 0; i < size; i++)
                _builder.Append((char)(26 * random.NextDouble() + _startChar));
            return _builder.ToString();
        }

        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public long GetTimeStamp()
        {
            return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
        }

        /// <summary>
        /// 获取随机串
        /// </summary>
        /// <returns></returns>
        public string NonceGet()
        {
            return NextString(15, true);
        }
    }
}
